package com.bdqn.t382.sz.test;

import com.bdqn.t382.sz.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Date;

public class Sz3 {
    public static void main(String[] args) {
        try {
            // 获取 Person 类的 Class 对象
            Class<?> personClass = Person.class;
            // 创建 Person 实例
            Constructor<?> constructor = personClass.getConstructor(String.class, int.class, Date.class);
            Date birthDate = new Date();
            Person person = (Person) constructor.newInstance("John", 30, birthDate);
            // 调用实例方法：无参方法 introduce()
            Method introduceMethod = personClass.getMethod("introduce");
            introduceMethod.invoke(person);
            // 调用实例方法：带参数的方法 celebrateBirthday(int years)
            Method celebrateBirthdayMethod = personClass.getMethod("celebrateBirthday", int.class);
            celebrateBirthdayMethod.invoke(person, 5);
            // 调用静态方法：sayHello()
            Method sayHelloMethod = personClass.getMethod("sayHello");
            sayHelloMethod.invoke(null); // 静态方法调用时，实例对象传 null
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
